/*  yALB - Yet Another List Builder. Roster Editor for Wargames/tabletop games.
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#ifndef YALB_H
#define YALB_H

#include <QMainWindow>
#include <QLabel>
#include <QCommandLinkButton>

#include "listsettings.h"
#include "informationdialog.h"
#include "validationdialog.h"

// Scripts
#include <QtScript>
#include <QScriptEngine>

// Plugins
#include <QPluginLoader>
#include "plugininterface.h"

#include "other/filestructs.h"
#include "collections/dualcollection.h"

class UnitItemDelegate;
class UnitListModel;
class OptionModel;
class ColumnHeader;
class Roster;
class RosterUnit;
class UnitItems;

namespace Ui {
    class yALB;
}

class yALB : public QMainWindow
{
    Q_OBJECT

public:
    explicit yALB(QWidget *parent = 0);
    ~yALB();

public slots:
    void showGlobalTextInformation(const QUrl& url);

private:
    Ui::yALB *ui;
    UnitItemDelegate *treeItemDelegate;
    QLabel *pointsLabel;
    ListSettings *listSettings;
    QTabBar *tabBar;
    QStringList validationErrors;
    InformationDialog informationDialog;
    ValidationDialog validationDialog;

    QCommandLinkButton *validationLink;

    //QPluginLoader *validatorPlugin;

    QScriptEngine *gameValidator;
    QScriptEngine *armyValidator;

    UnitListModel *unitListModel;
    OptionModel *optionModel;

    bool isSaved;

    GameFileData gameData;
    ArmyFileData armyData;

    const QString gameDataPath;

    QString currentArmy;
    QString currentGame;

    DualCollection<GameCategory> itemCategories;
    DualCollection<GameCategory> optionCategories;
    DualCollection<GameCategory> unitCategories;
    DualCollection<GameItem> items;
    DualCollection<GlobalText> globalTexts;
    DualCollection<UnitOption> options;

    Roster *roster;
    ColumnHeader *columnHeader;

    UnitItems *unitItemDialog;

    bool unsavedFileMessage();
    void setSaved(bool saved);

    bool loadGameAndArmy(const QString& gameFileName, const QString& armyFileName);
    bool loadArmyFile(const QString& fileName, bool clearAll);
    bool loadList(const QString& fileName);

    bool retrieveListSettings();

    QStringList itemToStringList(QTreeWidgetItem *item);
    void destroyingScript();

    void findGamesAndArmies();
    GameCategory getModifiedCategory(const InheritedCategoryData& data, const QString& key,
                                     const CategoryCollection& categories);

private slots:
    void initialize();

    void on_addButton_clicked();
    void tabChanged(int index);
    void on_actionDelete_triggered();
    void on_actionSaveList_triggered();

    void unitCategoryAdded(const GameCategory& element, const QString &key);

    void unitAdded(const Unit& unit, const QString &key);

    void columnAdded(const Column& column, const QString &key);

    void clearGameUnitCategories();
    void clearArmyUnitCategories();
    void on_unitList_doubleClicked(const QModelIndex &index);
    void rosterCurrentRowChanged(const QModelIndex &current, const QModelIndex &previous);
    void unitListCurrentRowChanged(const QModelIndex &current, const QModelIndex &previous);

    void optionCheckStateToChange(RosterUnit *unit, const QString& optionKey, Qt::CheckState targetState);
    void on_itemsButton_clicked();

    void setPointsText(double points);
    void updateValidationLink(const QSet<QString>& errors);

    void rosterRowsInserted(const QModelIndex &parent, int start, int end);
};

#endif // YALB_H
